package com.tb.foryou;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by 汪贵春 on 2016/12/28.
 */

public class LockActivity extends AppCompatActivity{

    private EditText etPassword;
    private String inputString;
    private String realPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lock_view);
        //获取真实密码
        realPassword = getResources().getString(R.string.password);
        //输入框设置
        etPassword = (EditText) findViewById(R.id.et_password);
        etPassword.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    String text = ((TextView) view).getText().toString();
                    if (!text.isEmpty()) {
                        inputString = text;
                        passwordDeal();
                    }
                    return true;
                }
                return false;
            }
        });
    }

    //验证密码
    private void passwordDeal(){
        if(inputString.equals(realPassword)){
            SharedPreferences sharedPreferences= getSharedPreferences("USER_IS_LOGIN", Activity.MODE_PRIVATE);
            //实例化SharedPreferences.Editor对象
            SharedPreferences.Editor editor = sharedPreferences.edit();
            //用putString的方法保存数据
            editor.putBoolean("USER_IS_LOGIN",true);
            //提交当前数据
            editor.apply();
            //关闭
            this.finish();
        }else {
            Toast.makeText(this,"密码错误哦😆",Toast.LENGTH_SHORT).show();
            etPassword.setText("");
        }
    }

}
